package com.pengtu.web.asset.equipment;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.entity.asset.equipment.EquipCheck;
import com.pengtu.entity.asset.equipment.EquipInformation;
import com.pengtu.service.asset.EquipmentManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;
@Namespace("/asset/equipment")
@Results({@Result(name=BaseActionSupport.RELOAD,location="check.action" ,type="redirect" )})
public class CheckAction extends BaseActionSupport<EquipCheck> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EquipmentManager equipManager;

	private String id;
	private EquipCheck equipCheck;
	private List<EquipCheck> checkList ;
	private Page<EquipCheck> page = new Page<EquipCheck>(10);
	private String equipId ;
	private  HttpSession session =Struts2Utils.getRequest().getSession();
	
	public void setEquipId(String equipId) {
		this.equipId = equipId;
	}

	public String getEquipId() {
		return equipId;
	}

	public Page<EquipCheck> getPage() {
		return page;
	}

	public List<EquipCheck> getCheckList() {
		return checkList;
	}

	@Autowired
	public void setEquipManager(EquipmentManager equipManager) {
		this.equipManager = equipManager;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public EquipCheck getModel() {
		// TODO Auto-generated method stub
		return equipCheck;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
//		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if(StringUtils.isEmpty(equipId))equipId=(String) session.getAttribute("equipId");
		page = equipManager.findFuzzyEquipCheck(page);
		if(StringUtils.isNotEmpty(equipId)){
		     checkList = equipManager.findEquipCheckList("equipInformation.id", equipId);
			if(checkList.size()>0){
				page.setResult(checkList);
				page.setTotalCount(checkList.size());
			}
		}
		
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return list();
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(equipId)){
			EquipInformation ekInformation = equipManager.getProperty("id", equipId);
			equipCheck.setEquipInformation(ekInformation);
		}
		equipManager.saveOrUpdateTest(equipCheck);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage("设备检查记录"+mes+"成功Q@!");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		equipManager.deleteTest(id);
		addActionMessage("设备检查记录被删除Q@!");
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		/**
		 * 将设备ID存放到session中
		 */
		 
			session.setAttribute("equipId", equipId);
			if(StringUtils.isNotEmpty(id)){
				equipCheck = equipManager.findPropertyTest("id", id);
			}else{
				equipCheck = new EquipCheck();
				}
	}
}
